package com.example.mongodb.controller;

import com.example.mongodb.pojo.Location;
import com.example.mongodb.pojo.Person;
import com.example.mongodb.repository.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashSet;
import java.util.List;

/**
 * @author ljw
 * @description
 * @date 2022/8/1 8:08
 */
@RestController
@RequestMapping("/person")
public class PersonController {

    @Autowired
    private PersonRepository personRepository;

    @RequestMapping("/save")
    private Person save() {
        Person person = new Person("wyf", 32);
        LinkedHashSet<Location> locations = new LinkedHashSet<>();
        locations.add(new Location("上海", "2009"));
        locations.add(new Location("合肥", "2010"));
        locations.add(new Location("广州", "2011"));
        locations.add(new Location("马鞍山", "2012"));

        person.setLocations(locations);
        return personRepository.save(person);
    }

    @GetMapping("/findByName")
    public Person findByName(@RequestParam String name) {
        return personRepository.findByName(name);
    }

    @GetMapping("/withQueryFindByAge")
    public List<Person> withQueryFindByAge(@RequestParam Integer age) {
        return personRepository.withQueryFindByAge(age);
    }
}
